package cn.iocoder.yudao.module.custom.controller.admin.merchant;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.custom.controller.admin.merchant.vo.*;
import cn.iocoder.yudao.module.custom.dal.dataobject.merchant.MerchantDO;
import cn.iocoder.yudao.module.custom.convert.merchant.MerchantConvert;
import cn.iocoder.yudao.module.custom.service.merchant.MerchantService;

/**
 * @author Administrator
 */
@Tag(name = "管理后台 - 商户信息")
@RestController
@RequestMapping("/custom/merchant")
@Validated
public class MerchantController {

    @Resource
    private MerchantService merchantService;

    @PostMapping("/create")
    @Operation(summary = "创建商户信息")
    @PreAuthorize("@ss.hasPermission('custom:merchant:create')")
    public CommonResult<Integer> createMerchant(@Valid @RequestBody MerchantCreateReqVO createReqVO) {
        return success(merchantService.createMerchant(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户信息")
    @PreAuthorize("@ss.hasPermission('custom:merchant:update')")
    public CommonResult<Boolean> updateMerchant(@Valid @RequestBody MerchantUpdateReqVO updateReqVO) {
        merchantService.updateMerchant(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('custom:merchant:delete')")
    public CommonResult<Boolean> deleteMerchant(@RequestParam("id") Integer id) {
        merchantService.deleteMerchant(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('custom:merchant:query')")
    public CommonResult<MerchantRespVO> getMerchant(@RequestParam("id") Integer id) {
        MerchantDO merchant = merchantService.getMerchant(id);
        return success(MerchantConvert.INSTANCE.convert(merchant));
    }


    @GetMapping("/page")
    @Operation(summary = "获得商户信息分页")
    @PreAuthorize("@ss.hasPermission('custom:merchant:query')")
    public CommonResult<PageResult<MerchantRespVO>> getMerchantPage(@Valid MerchantPageReqVO pageVO) {
        PageResult<MerchantDO> pageResult = merchantService.getMerchantPage(pageVO);
        return success(MerchantConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户信息 Excel")
    @PreAuthorize("@ss.hasPermission('custom:merchant:export')")
    @OperateLog(type = EXPORT)
    public void exportMerchantExcel(@Valid MerchantExportReqVO exportReqVO,
                                    HttpServletResponse response) throws IOException {
        List<MerchantDO> list = merchantService.getMerchantList(exportReqVO);
        // 导出 Excel
        List<MerchantExcelVO> datas = MerchantConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商户信息.xls", "数据", MerchantExcelVO.class, datas);
    }

}
